<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="持续集成：Continuous Integration，简称 CI，意思是，在一个项目中，任何人对代码库的任何改动，都会触发 CI 服务器自动对项目进行构建，自动运行测试，甚至自动部署到测试环境。这样做的好处就是，随时发现问题，随时修复。因为修复问题的成本随着时间的推移而增长，越早发现，修复成本越低。 下面来介绍如何为 GitHub 上托管的开源项目用 Travis CI 进行持续集成： trav">
<meta name="keywords" content="nodejs,travis,github">
<meta property="og:type" content="article">
<meta property="og:title" content="如何使用travis为nodejs持续集成">
<meta property="og:url" content="https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/index.html">
<meta property="og:site_name" content="月光倾城的代码小屋">
<meta property="og:description" content="持续集成：Continuous Integration，简称 CI，意思是，在一个项目中，任何人对代码库的任何改动，都会触发 CI 服务器自动对项目进行构建，自动运行测试，甚至自动部署到测试环境。这样做的好处就是，随时发现问题，随时修复。因为修复问题的成本随着时间的推移而增长，越早发现，修复成本越低。 下面来介绍如何为 GitHub 上托管的开源项目用 Travis CI 进行持续集成： trav">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://img.vim-cn.com/f5/77548bb55f147b0891d1a7fb8bab96f9e0335e.png">
<meta property="og:image" content="https://img.vim-cn.com/59/593ea99fea36afde85e75bcd40a7845731c1c4.png">
<meta property="og:image" content="https://img.vim-cn.com/31/f833ad262d987ab5db0f60eeeef504a50fe365.png">
<meta property="og:image" content="https://img.vim-cn.com/8f/9c86c53f7af5278b78739b7a02da3cd9f4d30d.png">
<meta property="og:image" content="https://img.vim-cn.com/df/8d1823372916f497b202c6aa29ccb75973dbc5.png">
<meta property="og:updated_time" content="2020-01-15T06:30:09.120Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="如何使用travis为nodejs持续集成">
<meta name="twitter:description" content="持续集成：Continuous Integration，简称 CI，意思是，在一个项目中，任何人对代码库的任何改动，都会触发 CI 服务器自动对项目进行构建，自动运行测试，甚至自动部署到测试环境。这样做的好处就是，随时发现问题，随时修复。因为修复问题的成本随着时间的推移而增长，越早发现，修复成本越低。 下面来介绍如何为 GitHub 上托管的开源项目用 Travis CI 进行持续集成： trav">
<meta name="twitter:image" content="https://img.vim-cn.com/f5/77548bb55f147b0891d1a7fb8bab96f9e0335e.png">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>如何使用travis为nodejs持续集成</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2017/12/05/nodejs_babel/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2017/11/08/mysql_query_learning/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&text=如何使用travis为nodejs持续集成"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&is_video=false&description=如何使用travis为nodejs持续集成"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=如何使用travis为nodejs持续集成&body=Check out this article: https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&name=如何使用travis为nodejs持续集成&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#travis-简介"><span class="toc-number">1.</span> <span class="toc-text">travis 简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#具体改怎么做"><span class="toc-number">2.</span> <span class="toc-text">具体改怎么做</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#自定义-travis-的集成步骤"><span class="toc-number">3.</span> <span class="toc-text">自定义 travis 的集成步骤</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#更好的方式"><span class="toc-number">4.</span> <span class="toc-text">更好的方式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#结尾"><span class="toc-number">5.</span> <span class="toc-text">结尾</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        如何使用travis为nodejs持续集成
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">月光倾城的代码小屋</span>
      </span>
      
    <div class="postdate">
        <time datetime="2017-12-05T10:24:29.000Z" itemprop="datePublished">2017-12-05</time>
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/github/">github</a>, <a class="tag-link" href="/tags/nodejs/">nodejs</a>, <a class="tag-link" href="/tags/travis/">travis</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <p><strong>持续集成</strong>：Continuous Integration，简称 CI，意思是，在一个项目中，任何人对代码库的任何改动，都会触发 CI 服务器自动对项目进行构建，自动运行测试，甚至自动部署到测试环境。这样做的好处就是，随时发现问题，随时修复。因为修复问题的成本随着时间的推移而增长，越早发现，修复成本越低。</p>
<p>下面来介绍如何为 GitHub 上托管的开源项目用 Travis CI 进行持续集成：</p>
<h4 id="travis-简介"><a href="#travis-简介" class="headerlink" title="travis 简介"></a>travis 简介</h4><p><code>Travis CI</code>是在线托管的 CI 服务，用<code>Travis</code>来进行持续集成，不需要自己搭服务器，在网页上点几下就好，用起来更方便。最重要的是，它对开源项目是免费的，而且天生对 github 项目友好，所以使用 travis 集成 githu 项目是最好不过了。<br><a href="https://travis-ci.com/" target="_blank" rel="noopener">travis 官网</a>，直接使用 github 登录，第一次登录之后需要对已有的 github 项目进行授权，你只需把你需要使用 travis 的项目打上勾就好了。默认情况下，travis 会在你 push 代码的时候会收到 github 的通知，然后为你执行部署的命令。</p>
<h4 id="具体改怎么做"><a href="#具体改怎么做" class="headerlink" title="具体改怎么做"></a>具体改怎么做</h4><p>先讲下我的需求：博主一直使用<code>hexo</code>来写博客，并且想让自己的博客可以使用两个域名来访问—<a href="https://andyliwr.github.io以及http://www.andylistudio.com，andylistudio的域名是指向一台阿里云服务器，在服务器上使用`hexo" target="_blank" rel="noopener">https://andyliwr.github.io以及http://www.andylistudio.com，andylistudio的域名是指向一台阿里云服务器，在服务器上使用`hexo</a> server`启动了一个本地 3000 端口，并使用 nginx 转发到 80。那么现在来想一下在编写完一篇博客之后我们需要做的操作：</p>
<ol>
<li>执行<code>hexo clean</code>、<code>hexo generate</code>、<code>hexo deploy</code>将代码推送到 github 远程仓库，这样过一会儿 github 会自动更新<a href="https://andyliwr.github.io">https://andyliwr.github.io</a></li>
<li>执行<code>git add .</code>、<code>git commit -am &quot;xxx&quot;</code>、<code>git push</code>将代码推送到管理博客的 github 的远程仓库，然后登陆阿里云的服务器，<code>cd</code>到管理博客的 github 项目根目录下，执行 git pull 拉取刚才的改动</li>
</ol>
<p>博主觉得这样一个流程太复杂，能不能用些简单的方法将所有的动作简化为一个动作—那就是我只需要执行<code>git push</code>，其他的事都不用管了。我想到了持续集成，正好之前也了解到了 travis，所以花了些时间学习了下<code>travis</code>，并且在历经磨难之后学会了<code>travis</code>☺。我突然想起了自己还在上海的时候，为了使用 jekyll 搭建自己的一个博客，整整花了一个周末折腾。</p>
<p>我们开始吧：</p>
<ol>
<li><strong>安装 travis 的运行环境</strong><br>首先我们需要在服务器上安装 travis 的运行环境，但是因为 travis 是基于<code>ruby</code>开发的，所以我们先要安装一个<code>ruby</code>。<code>ubuntu</code>安装<code>ruby</code>很简单</li>
</ol>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-<span class="builtin-name">get</span> update</span><br><span class="line">sudo apt-<span class="builtin-name">get</span> install ruby</span><br></pre></td></tr></table></figure>
<p><strong>PS</strong>:这样安装的 ruby 很多时候安装不上 travis，因为 travis 需要 ruby2.0 以上的版本，但是 apt-get 安装的是 1.9 版本。所以最好是选择源码安装</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 卸载之前安装的ruby</span></span><br><span class="line">sudo apt-get remove ruby</span><br><span class="line"><span class="meta">#</span><span class="bash"> ruby官方网站下载地址 http://www.ruby-lang.org/en/downloads/</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 先安装一些编译需要用到的包</span></span><br><span class="line">sudo apt-get -y update</span><br><span class="line">sudo apt-get - y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev</span><br><span class="line"><span class="meta">#</span><span class="bash"> 下载安装包并解压</span></span><br><span class="line">wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.3.tar.gz</span><br><span class="line">tar -zxvf  ruby-2.4.3.tar.gz</span><br><span class="line">cd ruby-2.4.3</span><br><span class="line">./configure --prefix=/usr/local</span><br><span class="line">make</span><br><span class="line">sudo make install</span><br><span class="line"><span class="meta">#</span><span class="bash"> 验证是否安装好了</span></span><br><span class="line">ruby -v</span><br></pre></td></tr></table></figure>
<p>然后是安装 travis，执行运行如下命令:</p>
<figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gem <span class="keyword">install</span> travis</span><br></pre></td></tr></table></figure>
<p><strong>PS</strong>: 如果安装太慢可以将<code>gem</code>源改成国内源，<a href="https://gems.ruby-china.org/" target="_blank" rel="noopener">参考方法</a></p>
<ol start="2">
<li><strong>使用 travis 初始化你的项目</strong><br>因为我们需要使用 travis 将<code>hexo generate</code>生成的代码推送到 github 上，所以需要让服务器拥有 github 推送的代码的权限:</li>
</ol>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成密钥</span></span><br><span class="line">ssh-keygen -t rsa -C <span class="string">"andyliwr@outlook.com"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开启ssh服务</span></span><br><span class="line">eval `ssh-agent -s`</span><br><span class="line"></span><br><span class="line"><span class="comment"># 添加密钥</span></span><br><span class="line">ssh-<span class="builtin-name">add</span> ~/.ssh/id_rsa.pub</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看刚才生成的公钥，并复制它，将它添加到GitHub的ssh key里去</span></span><br><span class="line">cat ~/.ssh/id_rsa.pub</span><br><span class="line"></span><br><span class="line"><span class="comment"># 在~/.ssh/添加一个ssh_config文件，输入如下内容</span></span><br><span class="line">Host github.com</span><br><span class="line"> <span class="built_in"> User </span>git</span><br><span class="line">  StrictHostKeyChecking <span class="literal">no</span></span><br><span class="line">  IdentityFile ~/.ssh/id_rsa</span><br><span class="line">  IdentitiesOnly <span class="literal">yes</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用ssh克隆项目，这里使用ssh地址</span></span><br><span class="line">git clone git@github.com:Andyliwr/blog_manager.git</span><br></pre></td></tr></table></figure>
<p>这样我们服务器就拥有了推送代码到 github 的权限。<br><strong>PS</strong>：腾讯云服务 ssh 秘钥只有 root 用户才有权限更改和查看，所以在创建秘钥的时候别忘了先将.ssh 目录的属主改成 ubuntu</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo chown <span class="string">ubuntu:</span>ubuntu <span class="regexp">/home/</span>ubuntu<span class="regexp">/.ssh/</span> -R</span><br></pre></td></tr></table></figure>
<ol start="3">
<li><strong>初始化 travis 配置文件</strong><br>在 github 项目根目录下执行如下命令：</li>
</ol>
<figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用 githu 登录 travis，期间需要输入github的用户名和密码</span></span><br><span class="line">travis login <span class="params">--auto</span></span><br><span class="line"><span class="comment"># 创建配置文件</span></span><br><span class="line">touch <span class="string">.travis.yml</span></span><br><span class="line"><span class="comment"># 使用travis加密你的ssh秘钥，会生成一个id_rsa.enc文件，并且会往.travis.yml里写入一个before_install的命令。这样你就能放心的将秘钥存储在github项目上了</span></span><br><span class="line">travis encrypt-file ~<span class="string">/.ssh/id_rsa</span> <span class="params">--add</span></span><br><span class="line"><span class="comment"># 移动下刚才生成的id_rsa.enc文件</span></span><br><span class="line">mkdir <span class="string">.travis</span> &amp;&amp; mv id_rsa.enc <span class="string">.travis/</span></span><br><span class="line"><span class="comment"># 修改before_install命令了的id_rsa.enc的路径为.travis/id_rsa.enc</span></span><br></pre></td></tr></table></figure>
<ol start="4">
<li><strong>初始化完成</strong><br>如此一个 travis 项目算是初始化好了，你可以执行一个代码<code>push</code>，然后去 travis 的官网看下持续集成是否执行成功了。</li>
</ol>
<h4 id="自定义-travis-的集成步骤"><a href="#自定义-travis-的集成步骤" class="headerlink" title="自定义 travis 的集成步骤"></a>自定义 travis 的集成步骤</h4><p>之前还是只初始化好了 travis，下面我来介绍如定义自定步骤：</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用的语言</span></span><br><span class="line"><span class="attr">language:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">node_js</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用的node版本</span></span><br><span class="line"><span class="attr">node_js:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">"8.0.0"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 仓库的分支</span></span><br><span class="line"><span class="attr">branches:</span></span><br><span class="line">  <span class="attr">only:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行isntall前需要执行的命令</span></span><br><span class="line"><span class="attr">before_install:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">openssl</span> <span class="string">aes-256-cbc</span> <span class="string">-K</span> <span class="string">$encrypted_d839c941aa8f_key</span> <span class="string">-iv</span> <span class="string">$encrypted_d839c941aa8f_iv</span></span><br><span class="line">  <span class="string">-in</span> <span class="string">.travis/id_rsa_travis.enc</span> <span class="string">-out</span> <span class="string">.travis/id_rsa_travis</span> <span class="string">-d</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 改变文件权限</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">chmod</span> <span class="number">600</span> <span class="string">.travis/id_rsa_travis</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置 ssh</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">eval</span> <span class="string">$(ssh-agent)</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">ssh-add</span> <span class="string">.travis/id_rsa_travis</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置git 的个人信息</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">config</span> <span class="string">--global</span> <span class="string">user.name</span> <span class="string">'andyliwr'</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">config</span> <span class="string">--global</span> <span class="string">user.email</span> <span class="string">andyliwr@outlook.com</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行install</span></span><br><span class="line"><span class="attr">install:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">npm</span> <span class="string">install</span> <span class="string">hexo-cli</span> <span class="string">-g</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">npm</span> <span class="string">install</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 执行命令脚本</span></span><br><span class="line"><span class="attr">script:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">hexo</span> <span class="string">clean</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">hexo</span> <span class="string">g</span> <span class="string">-d</span></span><br></pre></td></tr></table></figure>
<p><strong>PS</strong>：注意由于现在腾讯云服务器已经可以只用 ssh 秘钥访问 github 了所以在博客项目的<code>.config.yml</code>文件 deploy 里应该 ssh 地址我这里感觉还有些问题，直接将<code>id_rsa_travis</code>秘钥存储在了 github 项目上，那么别人如果拿到了这个秘钥就能对我的 github 做任意的修改。这个问题以后再修复吧~</p>
<h4 id="更好的方式"><a href="#更好的方式" class="headerlink" title="更好的方式"></a>更好的方式</h4><p>之前说过我是将<code>id_rsa_travis</code>秘钥直接存放在了 github 项目中，这样很不安全。果然第二天就有人给我提了个<code>pull request</code>，让我删除这个秘钥。我一想不能呀，删除之后我怎么把代码推送到<code>Andyliwr.github.io</code>这个项目呢？<br><img src="https://img.vim-cn.com/f5/77548bb55f147b0891d1a7fb8bab96f9e0335e.png" alt="Pull Request"><br>于是自己又去百度了下怎么使用<code>travis</code>部署<code>github</code>博客，没想到还真给找到了，<a href="https://www.jianshu.com/p/e22c13d85659" target="_blank" rel="noopener">手把手教你使用 Travis CI 自动部署你的 Hexo 博客到 Github 上</a></p>
<ol>
<li><strong>生成<code>github access token</code></strong><br>其实不使用<code>ssh</code>秘钥，还有一种方式让你能推送代码到<code>github</code>项目，那就是<code>github access token</code>。在<code>Setting</code>–&gt;<code>Developer settings</code>–&gt;<code>Personal access tokens</code>中点击<code>Generate new token</code>，生成一个<code>token</code>，权限选择上除了<code>delete_repo</code>其他都打上勾<br><img src="https://img.vim-cn.com/59/593ea99fea36afde85e75bcd40a7845731c1c4.png" alt="权限选择"></li>
<li><strong>将<code>token</code>加入到项目的环境变量中</strong><br>复制生成的<code>token</code>，然后打开<code>travis</code>官网找到博客项目，在<code>Setting</code>–&gt;<code>Environment Variables</code>中添加一个变量–<code>GITHUB_TOKEN</code>，值就是刚才生成的<code>token</code>。这样你就能在<code>.travis.yml</code>中通过<code>${GITHUB_TOKEN}</code>获取到这个值了之所以这样做是为了避免 token 值被提交到 github 项目中，产生一些不必要的麻烦。<br><img src="https://img.vim-cn.com/31/f833ad262d987ab5db0f60eeeef504a50fe365.png" alt="设置环境变量"></li>
<li><strong>配置<code>.travis.yml</code>文件</strong><br>这里需要将<code>hexo d</code>的部署方式替换成<code>git push --force</code>的方式，因为没有了<code>id_rsa_travis</code>之后<code>hexo d</code>的部署方式需要输入<code>github</code>用户名和密码。其他没啥区别，我直接贴代码了:</li>
</ol>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">language:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">node_js</span></span><br><span class="line"><span class="attr">node_js:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">stable</span></span><br><span class="line"></span><br><span class="line"><span class="attr">branches:</span></span><br><span class="line">  <span class="attr">only:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line"><span class="attr">install:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">npm</span> <span class="string">install</span> <span class="string">hexo-cli</span> <span class="string">-g</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">npm</span> <span class="string">install</span></span><br><span class="line"></span><br><span class="line"><span class="attr">script:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">hexo</span> <span class="string">clean</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">hexo</span> <span class="string">g</span></span><br><span class="line"></span><br><span class="line"><span class="attr">after_script:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">cd</span> <span class="string">./public</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">init</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">config</span> <span class="string">user.name</span> <span class="string">"andyliwr"</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">config</span> <span class="string">user.email</span> <span class="string">"andyliwr@outlook.com"</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">add</span> <span class="string">.</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">commit</span> <span class="string">-m</span> <span class="string">"Update blog"</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">git</span> <span class="string">push</span> <span class="string">--force</span> <span class="string">--quiet</span> <span class="string">"https://$&#123;GITHUB_TOKEN&#125;@$&#123;GH_REF&#125;"</span> <span class="string">master:master</span></span><br><span class="line"></span><br><span class="line"><span class="attr">env:</span></span><br><span class="line"><span class="attr">global:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="attr">GH_REF:</span> <span class="string">github.com/Andyliwr/Andyliwr.github.io</span></span><br></pre></td></tr></table></figure>
<h4 id="结尾"><a href="#结尾" class="headerlink" title="结尾"></a>结尾</h4><p>其实如何登陆我的腾讯云服务器，帮我执行<code>git pull</code>启动，我还是不知道怎么做。不过本人其实打算放弃<code>andylistudio.com</code>的域名了，以后的博客域名就是用<code>andyliwr.github.io</code>吧~<br>最后贴一张，<code>travis</code>部署的执行结果:<br><img src="https://img.vim-cn.com/8f/9c86c53f7af5278b78739b7a02da3cd9f4d30d.png" alt="任务集合"><br><img src="https://img.vim-cn.com/df/8d1823372916f497b202c6aa29ccb75973dbc5.png" alt="日志输出"></p>

  </div>
  <div class="statement">
    <hr>  
    1. 如果本文帮到了您，不妨点一下右上角的”<i class="fas fa-share-alt"></i>“按钮。<br>
    2. 除非注明，本博文章均为原创，转载请以链接形式标明本文地址。<br>
    3. 如果你有任何疑问，欢迎发送邮件到我的邮箱<a href="mailto:andyliwr@outlook.com">andyliwr@outlook.com<br>
  </div>
</article>


    <div class="blog-post-comments">
        <div id="gitalk_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#travis-简介"><span class="toc-number">1.</span> <span class="toc-text">travis 简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#具体改怎么做"><span class="toc-number">2.</span> <span class="toc-text">具体改怎么做</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#自定义-travis-的集成步骤"><span class="toc-number">3.</span> <span class="toc-text">自定义 travis 的集成步骤</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#更好的方式"><span class="toc-number">4.</span> <span class="toc-text">更好的方式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#结尾"><span class="toc-number">5.</span> <span class="toc-text">结尾</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&text=如何使用travis为nodejs持续集成"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&is_video=false&description=如何使用travis为nodejs持续集成"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=如何使用travis为nodejs持续集成&body=Check out this article: https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&title=如何使用travis为nodejs持续集成"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/&name=如何使用travis为nodejs持续集成&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Dikang Li
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
</body>
</html>
<!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">
<link rel="stylesheet" href="/lib/fancybox/jquery.fancybox.css">

<link rel="stylesheet" href="https://file.lantingshucheng.com/blog/v3/static/styles/gitalk.css">


<!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<script src="/lib/fancybox/jquery.fancybox.pack.js"></script>

<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/md5.js"></script>
<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/gitalk.min.js"></script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?7851ca7a86be957be994acb77639a5be";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


  <script type="text/javascript">
    window.onload = function(){
      if (window.location.href.indexOf('andyliwr.github.io') > -1) {
        var gitalk = new Gitalk({
          clientID: 'ad6f70df0c70d2b715f9',
          clientSecret: '40384bee244c1e17dd7c37733d21e9ef669e969c',
          repo: 'Andyliwr.github.io',
          owner: 'Andyliwr',
          admin: ['Andyliwr'],
          id: md5(location.pathname),
          distractionFreeMode: 'true'
        })
        gitalk.render('gitalk_thread')
      }
    }
  </script>


